package com.mystudy.springcloud.service;

import com.mystudy.springcloud.utils.IdGeneratorSnowflake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class SonwflakeService {
	@Autowired
	IdGeneratorSnowflake idGeneratorSnowflake;

	public List<Long> snowflakeId() {
		ExecutorService threadPool = Executors.newFixedThreadPool(5);
		List<Long> list = new CopyOnWriteArrayList<>();
		for (int i = 0; i < 20; i++) {
			threadPool.submit(() -> {
				list.add(idGeneratorSnowflake.snowflakeId());
			});
		}
		threadPool.shutdown();
		return list;
	}

	public List<Long> snowflakeId(long workerId, long datacenterId) {
		ExecutorService threadPool = Executors.newFixedThreadPool(5);
		List<Long> list = new CopyOnWriteArrayList<>();
		for (int i = 0; i < 20; i++) {
			threadPool.submit(() -> {
				list.add(idGeneratorSnowflake.snowflakeId(workerId, datacenterId));
			});
		}
		threadPool.shutdown();
		return list;
	}
}
